/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sioc.aop;

import com.jspx.utils.ArrayUtil;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-2-14
 * Time: 0:22:53
 */
public class MethodKey
{
    private MethodKey()
    {

    }

    public static String getCacheKey(Object proxy, Method method, Object[] args)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(proxy.getClass().getName()).append("_").append(method.getName());
        if (args != null)
        {
            for (Object argument : args)
            {
                if (argument == null)
                {
                    sb.append("_").append("NULL");
                } else if (argument instanceof String[])
                {
                    String[] argumentArray = (String[]) argument;
                    sb.append("_").append(ArrayUtil.toString(argumentArray, "_"));
                } else if (argument instanceof int[])
                {
                    int[] argumentArray = (int[]) argument;
                    sb.append("_").append(ArrayUtil.toString(argumentArray, "_"));
                } else if (argument instanceof List)
                {
                    List list = (List) argument;
                    StringBuilder childSb = new StringBuilder();
                    for (Object o : list)
                    {
                        childSb.append(o).append("_");
                    }
                    sb.append("_").append(childSb.toString());
                } else if (argument instanceof Map)
                {
                    Map map = (Map) argument;
                    StringBuilder childSb = new StringBuilder();
                    for (Object o : map.keySet())
                    {
                        childSb.append(o).append(map.get(o)).append("_");
                    }
                    sb.append("_").append(childSb.toString());
                } else if (argument instanceof Object[])
                {
                    StringBuilder childSb = new StringBuilder();
                    Object[] argumentArray = (Object[]) argument;
                    for (Object o : argumentArray)
                    {
                        childSb.append(o).append("_");
                    }
                    sb.append("_").append(childSb.toString());
                } else
                {
                    sb.append("_").append(argument);
                }
            }
        }
        return sb.toString();
    }
}